package net.stogi.dsper.ui.client.module.datasource;

import net.stogi.common.mvp.core.behavior.Behavior;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;

class DataSourceEditorWidget extends Composite implements DataSourceEditorView {

	private static final FormData DEFAULT_FIELD_LAYOUT_DATA = new FormData("-20");

	private final Button cancelButton = new Button();

	private final FormPanel mainPanel = new FormPanel();

	private final FormButtonBinding mainPanelButtonBinding = new FormButtonBinding(mainPanel);

	private final TextField<String> nameTextField = new TextField<String>();

	private final Button saveButton = new Button();

	private final TextField<String> urlTextField = new TextField<String>();

	public DataSourceEditorWidget() {
		initBinding();
		initComposite();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void appear() {
		mainPanel.show();
		// mainPanel.el().fadeIn(FxConfig.NONE);
	}

	@Override
	public void block() {
		mainPanel.mask();
	}

	@Override
	public String collectName() {
		return nameTextField.getValue();
	}

	@Override
	public String collectUrl() {
		return urlTextField.getValue();
	}

	@Override
	public void disappear() {
		// mainPanel.el().fadeOut(FxConfig.NONE);
		mainPanel.hide();
	}

	@Override
	public void displayName(String name) {
		nameTextField.setValue(name);
	}

	@Override
	public void displayUrl(String url) {
		urlTextField.setValue(url);
	}

	@Override
	public void setCancelBehavior(final Behavior behavior) {
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setSaveBehavior(final Behavior behavior) {
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void unblock() {
		mainPanel.unmask();
	}

	private void initBinding() {
		mainPanelButtonBinding.addButton(saveButton);
	}

	private void initComposite() {
		cancelButton.setText("Cancel");
		saveButton.setText("Save");

		nameTextField.setAllowBlank(false);
		nameTextField.setFieldLabel("Name");
		urlTextField.setAllowBlank(false);
		urlTextField.setFieldLabel("URL");

		mainPanel.setHeading("Data Source Editor");
		mainPanel.setFrame(true);
		// Width is required because DEFAULT_FIELD_LAYOUT_DATA do not work with
		// fixed width.
		mainPanel.setWidth("100%");
		mainPanel.setVisible(false);
	}

	private void initLayout() {
		mainPanel.add(nameTextField, DEFAULT_FIELD_LAYOUT_DATA);
		mainPanel.add(urlTextField, DEFAULT_FIELD_LAYOUT_DATA);
		mainPanel.addButton(cancelButton);
		mainPanel.addButton(saveButton);
	}
}
